﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class MineFiledLevel : Level
{
    public CollsionCallback m_RecycleRegion;
    public AudioSource m_RecycleSound;
    public AudioSource m_UnrecycleSound;

    List<Stone> m_RecycleStones = new List<Stone>();
    protected void Awake()
    {
        m_RecycleRegion.OnTrigger2DEnterCallback = OnRecycleRegionEnter;
        m_RecycleRegion.OnTrigger2DExitCallback = OnRecycleRegionLeave;
    }
    protected override void Start()
    {
        base.Start();
    }
    protected override void Update()
    {
        base.Update();
    }
    public virtual void OnStart()
    {
    }

    public override void OnSucceed()
    {
        for(int i=0; i< m_RecycleStones.Count; ++i)
        {
            if(m_RecycleStones[i].m_StoneLevel == Stone.StoneLevel.LEVLE_1 )
            {
                if(ItemFactory.Instance.IsMaterial(ItemFactory.Instance.GetItemType( m_RecycleStones[i].m_Kind)))
                    m_GetItems.Add(ItemFactory.Instance.createItem(m_RecycleStones[i].m_Kind.ToString()));
            }
            else if(m_RecycleStones[i].m_StoneLevel > Stone.StoneLevel.LEVLE_1)
            {
                Player.Instance.addShip(m_RecycleStones[i] as Ship);
            }
        }

        base.OnSucceed();
        MessageShow.Instance.Messaage("大型石头都放在船坞了，你可以在商店出售这些大型石头。", new Vector4(0, 1, 0, 1));
    }
    protected override void OnLose()
    {
        base.OnLose();
    }

    void OnRecycleRegionEnter(Collider2D collider)
    {
        var obj = collider.gameObject;
        var stone = obj.GetComponent<Stone>();
        if(!stone)
        {
            return;
        }
        m_RecycleStones.Add(stone);
        MessageShow.Instance.Messaage("回收矿石:\"" + stone.m_ShipStruct.m_Name + "\"", new Vector4(0, 1, 0, 1));
        if (m_RecycleSound)
            m_RecycleSound.Play();
    }
    void OnRecycleRegionLeave(Collider2D collider)
    {
        var obj = collider.gameObject;
        var stone = obj.GetComponent<Stone>();
        if (!stone)
        {
            return;
        }
        m_RecycleStones.Remove(stone);
        MessageShow.Instance.Messaage("移除矿石:\"" + stone.m_ShipStruct.m_Name+"\"", new Vector4(1, 0.3f, 0.5f, 1));
        if (m_UnrecycleSound)
            m_UnrecycleSound.Play();
    }

    public void OnClose()
    {
        OnSucceed();
    }
}